﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace soho.Library
{
    public partial class CustomerUtl
    {
        public static Customer GetCustomer(string username)
        {
            using (sohoDataClassesDataContext db = new sohoDataClassesDataContext(sohoConnection.ConnectionString))
            {
                var result =
                 (from c in db.Customers
                  where c.UserName == username
                  select c);
                if (result.Count<Customer>() == 0)
                    return null;
                else

                    return result.First();
            }
        }
        public static void Insert(Customer customer)
        {
            using (sohoDataClassesDataContext dataContext = new sohoDataClassesDataContext(sohoConnection.ConnectionString))
            {
                if (customer.UserName.Length > 0)
                {
                    dataContext.Customers.InsertOnSubmit(customer);
                }
                dataContext.SubmitChanges();
            }

        }
        /// <summary>
        /// Login
        /// 0 Success
        /// 1 Wrong username
        /// 2 Wrong password
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>0 1 2</returns>
        
        public static int Login(string username, string password)
        {
            using (sohoDataClassesDataContext db = new sohoDataClassesDataContext(sohoConnection.ConnectionString))
            {
                var result =
                 (from c in db.Customers
                  where c.UserName == username
                  select c);
                if (result.Count<Customer>() == 0)
                    return 1;//wrong username
                else
                {
                     result =
                 (from c in db.Customers
                  where c.UserName == username && c.Password== password
                  
                  select c);
                    if (result.Count<Customer>() == 0)
                        return 2;//wrong password
                }

                return 0;//success
            }
        }
        public static void Update(Customer customer)
        {
            using (sohoDataClassesDataContext dataContext = new sohoDataClassesDataContext(sohoConnection.ConnectionString))
            {
                var result =
                 (from c in dataContext.Customers
                  where c.UserName == customer.UserName
                  select c);
                Customer cus = result.First();
                cus.UserName = customer.UserName;
                cus.PhoneNumber = customer.PhoneNumber;
                cus.FullName = customer.FullName;
                cus.Address = customer.Address;
                cus.CompanyName = customer.CompanyName;
                cus.Email = customer.Email;
                dataContext.SubmitChanges();
                
            }

        }
    }
}
